﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Microsoft.Windows.Controls.Ribbon;
using Rifts.Creator;

namespace Rifts
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : RibbonWindow
	{
		public MainWindow()
		{
			InitializeComponent();
			
			DataContext = new PrimaryViewModel();
			TypedDataContext.PropertyChanged += TypedDataContext_PropertyChanged;
			Loaded += MainWindow_Loaded;
		}

		void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			UpdateContentFrame();
		}



		void TypedDataContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "LoadedContent")
				UpdateContentFrame();
		}

		RibbonTab m_ContextMenu;

		void UpdateContentFrame()
		{
			if (TypedDataContext.LoadedContent == null)
			{
				ContentFrame.Child = null;
				RibbonContextMenu = null;
			}
			else if (TypedDataContext.LoadedContent is CreatorViewModel)
			{
				ContentFrame.Child = new CreatorView() { DataContext = TypedDataContext.LoadedContent };
				RibbonContextMenu = new Creator.CreatorMenu() { DataContext = TypedDataContext.LoadedContent };
				RibbonContextMenu.IsSelected = true;
			}
		}

		public RibbonTab RibbonContextMenu
		{
			get { return m_ContextMenu; }
			set
			{
				if(m_ContextMenu != null)
					Ribbon.Items.Remove (m_ContextMenu);

				m_ContextMenu = value;

				if (m_ContextMenu != null)
					Ribbon.Items.Add(m_ContextMenu);
			}
		}
		public PrimaryViewModel TypedDataContext
		{
			get { return (PrimaryViewModel)DataContext; }
		}

		private void NewButton_Click(object sender, RoutedEventArgs e)
		{
			TypedDataContext.CreateNew();
		}

	}
}
